
var SERVER_PORT = "http://120.77.223.88:8080/red/";

var goodDetail = new Vue({
    el: "#app-gd",
    data: {
        //登录账户
        isLogin:false,
        phone: "",
        nickname:"",

        //商品信息
        good_info: {},

        //购买数量
        numToBuy: 1,

        //得到所有目录
        primaryCategory: [],

        isCollected: false,

        searchContent:"",

    },
    methods: {
        //退出登录
        quitLogin: function () {
            this.isLogin = false;
            this.phone = "";
            this.nickname = "";
            window.localStorage.setItem("isLogin", false);
            window.location.reload(true);

            window.localStorage.setItem
        },


        getAllCategory: function () {
            this.$http.get(SERVER_PORT + "requestCategory").then(
                response => {
                    //console.log(response.data.data);
                    var originalChaoticInfo = response.data.data;

                    for (var index in originalChaoticInfo) {
                        var primaryCategoryName = originalChaoticInfo[index].category1_Name;
                        var primaryCategoryID = originalChaoticInfo[index].category1_ID;

                        var primaryCategoryItem = {
                            name: primaryCategoryName,
                            ID: primaryCategoryID
                        }

                        // var secondCategory = [];

                        // for (var sub_index in originalChaoticInfo[index].category2) {
                        //     var secondCategoryName =
                        //         originalChaoticInfo[index].category2[sub_index].name;
                        //     var category2_ID =
                        //         originalChaoticInfo[index].category2[sub_index].category2_ID;
                        //     secondCategory.push({
                        //         name: secondCategoryName,
                        //         ID: category2_ID
                        //     });
                        // }

                        // this.categoryTree.push({
                        //     primaryCategory: primaryCategory,
                        //     secondCategory: secondCategory
                        // });
                        this.primaryCategory.push(primaryCategoryItem);
                    }

                },
                response => {
                    // 响应错误回调
                    alert("请求失败");
                }
            );
        },

        requestGoodInfoById: function (ID) {
            //console.log(ID)
            this.$http.post(SERVER_PORT + "requestGoods", { goods_ID: ID }, {
                emulateJSON: true
            }).then((response) => {
                //console.log(response.data.data);
                this.good_info = response.data.data[0];
            }, (error) => {
                alert("请求失败");
            })
        },

        changeNumToBuy: function (op_num) {
            if (this.numToBuy <= 0 && op_num == -1)
                return;
            if (this.numToBuy > 9 && op_num == 1)
                return;
            this.numToBuy += op_num;
        },

        addToCart: function () {

            if (!this.isLogin) {
                Ply.dialog("confirm", "请先登录").done(function () {
                    window.location.href = "login.html"
                });
                return;
            }
            // Cookies.remove("isLogin");
            // Cookies.remove("phone");
            // Cookies.remove("password");
            // console.log("click");
            var phone = window.localStorage.getItem("phone");
            var goods_ID = this.good_info.goods_ID;
            var quantity = this.numToBuy;

            this.$http.post(SERVER_PORT + "addGoodsInStore", {
                goods_ID: goods_ID,
                phone: phone,
                quantity: quantity,
            }, { emulateJSON: true }).then((response) => {
                Ply.dialog("alert", "加入成功").done(function () {
                    Cookies.remove("good_id");
                    window.location.href = "buy-cart.html";
                });
            }, (error) => {
                alert("请求失败")
            });


        },

        collect: function () {

            if (!this.isLogin) {
                Ply.dialog("confirm", "请先登录").done(function () {
                    window.location.href = "login.html"
                });
                return;
            }
            // Cookies.remove("isLogin");
            // Cookies.remove("phone");
            // Cookies.remove("password");
            // console.log("click");
            var phone = window.localStorage.getItem("phone");
            var goods_ID = this.good_info.goods_ID;


            if (this.isCollected != true) {

              
                this.$http.post(SERVER_PORT + "collectGoodsInStore", {
                    goods_ID: goods_ID,
                    phone: phone, }, { emulateJSON: true }).then((response) => {
                    this.isCollected = true;
                    Ply.dialog("alert", "收藏成功").done(function () {
                        document.querySelector("span[title]").innerText = "已收藏";
                        document.getElementsByClassName("collect-product")[0].getElementsByTagName("img")[0].setAttribute("src", "images/pro-detail/not-collect.png");
                        
                    });
                }, (error) => {
                    alert("请求失败")
                });
            }
            else {
                this.$http.post(SERVER_PORT + "deleteGoodsInCollect", {
                    goods_ID: goods_ID,
                    phone: phone,
                }, { emulateJSON: true }).then((response) => {
                    this.isCollected=false;
                    Ply.dialog("alert", "取消收藏成功").done(function () {
                        document.querySelector("span[title]").innerText = "收藏商品";
                        document.getElementsByClassName("collect-product")[0].getElementsByTagName("img")[0].setAttribute("src", "images/pro-detail/collect.png");
                    });
                }, (error) => {
                    alert("请求失败")
                });
            }


        },

        onPrimCateClick: function (index) {
            console.log(index);
            window.localStorage.setItem("primIndex_goodCart", index);
            window.location.href = "searchResult.html"
        },

        search:function(){

            if(this.searchContent.length==0)
            {
                Ply.dialog("alert", "输入不能为空").done(function () {
                });
                return;
            }
            window.localStorage.setItem("searchContent",this.searchContent);
            window.location.href="searchResult.html";
        }
    },
    created() {
        if (window.localStorage.getItem("isLogin") !== undefined && window.localStorage.getItem("isLogin") !== "false") {
            this.isLogin = true;
            this.nickname = window.localStorage.getItem("nickname");
            this.phone = window.localStorage.getItem('phone');
        }

        //var ID = window.localStorage.getItem("good_id");
        var ID = window.localStorage.getItem("good_id");
        this.getAllCategory();
        this.requestGoodInfoById(ID);
    },

})